package router

import (
	"errors"

	"gddgame.cc/galaxy/binocle"
	"gddgame.cc/galaxy/project/breeder/breeder/center"
	"gddgame.cc/galaxy/project/breeder/breeder/controller"
	"gddgame.cc/galaxy/project/breeder/breeder/cooperated"
	"gddgame.cc/galaxy/server/composer"
	"gddgame.cc/galaxy/utils/def"

	. "gddgame.cc/galaxy/common/dsl/web"
)

func ManagerRouterWith(builder composer.Builder, r *SwaggerRouter) error {
	var agent binocle.App
	builder.Factory("agent", &agent)

	r.Post("/unit/upgrade", Swagger{
		Controller: func(context def.Context, userUnitId string, level int) error {
			unit := controller.Center.TmpData(cooperated.UnitType, userUnitId)
			if unit == nil {
				return errors.New("部队不存在")
			}
			mapNoId := unit.ToString(cooperated.MapNoId)
			controller.Center.Signal(mapNoId).Sync(center.UnitUpgradeThresholdEvent, cooperated.UnitType, userUnitId, []interface{}{level})
			return nil
		},
		Params: []Param{
			{Name: `userUnitId`, In: Body, Required: true},
			{Name: `level`, In: Body, Required: true},
		},
		Response: ApiResponse,
	})
	return nil
}
